home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / pyxmpp / jabber / muc.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  17KB  |  507 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __revision__ = '$Id: muc.py 647 2006-08-26 18:27:39Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import logging
  7. from pyxmpp.presence import Presence
  8. from pyxmpp.message import Message
  9. from pyxmpp.iq import Iq
  10. from pyxmpp.jid import JID
  11. from pyxmpp.xmlextra import xml_element_ns_iter
  12. from pyxmpp.jabber.muccore import MucPresence, MucUserX, MucItem, MucStatus
  13. from pyxmpp.jabber.muccore import MUC_OWNER_NS
  14. from pyxmpp.jabber.dataforms import DATAFORM_NS, Form
  15. import weakref
  16.  
  17. class MucRoomHandler:
  18.     
  19.     def __init__(self):
  20.         self.room_state = None
  21.         self._MucRoomHandler__logger = logging.getLogger('pyxmpp.jabber.MucRoomHandler')
  22.  
  23.     
  24.     def assign_state(self, state_obj):
  25.         self.room_state = state_obj
  26.  
  27.     
  28.     def room_created(self, stanza):
  29.         _unused = stanza
  30.         self.room_state.request_instant_room()
  31.  
  32.     
  33.     def configuration_form_received(self, form):
  34.         pass
  35.  
  36.     
  37.     def room_configured(self):
  38.         pass
  39.  
  40.     
  41.     def user_joined(self, user, stanza):
  42.         pass
  43.  
  44.     
  45.     def user_left(self, user, stanza):
  46.         pass
  47.  
  48.     
  49.     def role_changed(self, user, old_role, new_role, stanza):
  50.         pass
  51.  
  52.     
  53.     def affiliation_changed(self, user, old_aff, new_aff, stanza):
  54.         pass
  55.  
  56.     
  57.     def nick_change(self, user, new_nick, stanza):
  58.         pass
  59.  
  60.     
  61.     def nick_changed(self, user, old_nick, stanza):
  62.         pass
  63.  
  64.     
  65.     def presence_changed(self, user, stanza):
  66.         pass
  67.  
  68.     
  69.     def subject_changed(self, user, stanza):
  70.         pass
  71.  
  72.     
  73.     def message_received(self, user, stanza):
  74.         pass
  75.  
  76.     
  77.     def room_configuration_error(self, stanza):
  78.         self.error(stanza)
  79.  
  80.     
  81.     def error(self, stanza):
  82.         err = stanza.get_error()
  83.         self._MucRoomHandler__logger.debug('Error from: %r Condition: %r' % (stanza.get_from(), err.get_condition))
  84.  
  85.  
  86.  
  87. class MucRoomUser:
  88.     
  89.     def __init__(self, presence_or_user_or_jid):
  90.         if isinstance(presence_or_user_or_jid, MucRoomUser):
  91.             self.presence = presence_or_user_or_jid.presence
  92.             self.role = presence_or_user_or_jid.role
  93.             self.affiliation = presence_or_user_or_jid.affiliation
  94.             self.room_jid = presence_or_user_or_jid.room_jid
  95.             self.real_jid = presence_or_user_or_jid.real_jid
  96.             self.nick = presence_or_user_or_jid.nick
  97.             self.new_nick = None
  98.         else:
  99.             self.affiliation = 'none'
  100.             self.presence = None
  101.             self.real_jid = None
  102.             self.new_nick = None
  103.             if isinstance(presence_or_user_or_jid, JID):
  104.                 self.nick = presence_or_user_or_jid.resource
  105.                 self.room_jid = presence_or_user_or_jid
  106.                 self.role = 'none'
  107.             elif isinstance(presence_or_user_or_jid, Presence):
  108.                 self.nick = None
  109.                 self.room_jid = None
  110.                 self.role = 'participant'
  111.                 self.update_presence(presence_or_user_or_jid)
  112.             else:
  113.                 raise TypeError, 'Bad argument type for MucRoomUser constructor'
  114.  
  115.     
  116.     def update_presence(self, presence):
  117.         self.presence = MucPresence(presence)
  118.         t = presence.get_type()
  119.         if t == 'unavailable':
  120.             self.role = 'none'
  121.             self.affiliation = 'none'
  122.         
  123.         self.room_jid = self.presence.get_from()
  124.         self.nick = self.room_jid.resource
  125.         mc = self.presence.get_muc_child()
  126.         if isinstance(mc, MucUserX):
  127.             items = mc.get_items()
  128.             for item in items:
  129.                 if not isinstance(item, MucItem):
  130.                     continue
  131.                 
  132.                 if item.role:
  133.                     self.role = item.role
  134.                 
  135.                 if item.affiliation:
  136.                     self.affiliation = item.affiliation
  137.                 
  138.                 if item.jid:
  139.                     self.real_jid = item.jid
  140.                 
  141.                 if item.nick:
  142.                     self.new_nick = item.nick
  143.                 
  144.                 break
  145.             
  146.         
  147.  
  148.     
  149.     def same_as(self, other):
  150.         return self.room_jid == other.room_jid
  151.  
  152.  
  153.  
  154. class MucRoomState:
  155.     
  156.     def __init__(self, manager, own_jid, room_jid, handler):
  157.         self.own_jid = own_jid
  158.         self.room_jid = room_jid
  159.         self.handler = handler
  160.         self.manager = weakref.proxy(manager)
  161.         self.joined = False
  162.         self.subject = None
  163.         self.users = { }
  164.         self.me = MucRoomUser(room_jid)
  165.         self.configured = None
  166.         self.configuration_form = None
  167.         handler.assign_state(self)
  168.         self._MucRoomState__logger = logging.getLogger('pyxmpp.jabber.MucRoomState')
  169.  
  170.     
  171.     def get_user(self, nick_or_jid, create = False):
  172.         if isinstance(nick_or_jid, JID):
  173.             if not nick_or_jid.resource:
  174.                 return None
  175.             
  176.             for u in self.users.values():
  177.                 if nick_or_jid in (u.room_jid, u.real_jid):
  178.                     return u
  179.                     continue
  180.             
  181.             if create:
  182.                 return MucRoomUser(nick_or_jid)
  183.             else:
  184.                 return None
  185.         
  186.         return self.users.get(nick_or_jid)
  187.  
  188.     
  189.     def set_stream(self, stream):
  190.         _unused = stream
  191.         if self.joined and self.handler:
  192.             self.handler.user_left(self.me, None)
  193.         
  194.         self.joined = False
  195.  
  196.     
  197.     def join(self, password = None, history_maxchars = None, history_maxstanzas = None, history_seconds = None, history_since = None):
  198.         if self.joined:
  199.             raise RuntimeError, 'Room is already joined'
  200.         
  201.         p = MucPresence(to_jid = self.room_jid)
  202.         p.make_join_request(password, history_maxchars, history_maxstanzas, history_seconds, history_since)
  203.         self.manager.stream.send(p)
  204.  
  205.     
  206.     def leave(self):
  207.         if self.joined:
  208.             p = MucPresence(to_jid = self.room_jid, stanza_type = 'unavailable')
  209.             self.manager.stream.send(p)
  210.         
  211.  
  212.     
  213.     def send_message(self, body):
  214.         m = Message(to_jid = self.room_jid.bare(), stanza_type = 'groupchat', body = body)
  215.         self.manager.stream.send(m)
  216.  
  217.     
  218.     def set_subject(self, subject):
  219.         m = Message(to_jid = self.room_jid.bare(), stanza_type = 'groupchat', subject = subject)
  220.         self.manager.stream.send(m)
  221.  
  222.     
  223.     def change_nick(self, new_nick):
  224.         new_room_jid = JID(self.room_jid.node, self.room_jid.domain, new_nick)
  225.         p = Presence(to_jid = new_room_jid)
  226.         self.manager.stream.send(p)
  227.  
  228.     
  229.     def get_room_jid(self, nick = None):
  230.         if nick is None:
  231.             return self.room_jid
  232.         
  233.         return JID(self.room_jid.node, self.room_jid.domain, nick)
  234.  
  235.     
  236.     def get_nick(self):
  237.         return self.room_jid.resource
  238.  
  239.     
  240.     def process_available_presence(self, stanza):
  241.         fr = stanza.get_from()
  242.         if not fr.resource:
  243.             return None
  244.         
  245.         nick = fr.resource
  246.         user = self.users.get(nick)
  247.         if user:
  248.             old_user = MucRoomUser(user)
  249.             user.update_presence(stanza)
  250.             user.nick = nick
  251.         else:
  252.             old_user = None
  253.             user = MucRoomUser(stanza)
  254.             self.users[user.nick] = user
  255.         self.handler.presence_changed(user, stanza)
  256.         if fr == self.room_jid and not (self.joined):
  257.             self.joined = True
  258.             self.me = user
  259.             mc = stanza.get_muc_child()
  260.             if self.configured is None:
  261.                 self.configured = True
  262.             
  263.         
  264.         if not old_user or old_user.role == 'none':
  265.             self.handler.user_joined(user, stanza)
  266.         elif old_user.nick != user.nick:
  267.             self.handler.nick_changed(user, old_user.nick, stanza)
  268.             if old_user.room_jid == self.room_jid:
  269.                 self.room_jid = fr
  270.             
  271.         
  272.         if old_user.role != user.role:
  273.             self.handler.role_changed(user, old_user.role, user.role, stanza)
  274.         
  275.         if old_user.affiliation != user.affiliation:
  276.             self.handler.affiliation_changed(user, old_user.affiliation, user.affiliation, stanza)
  277.         
  278.  
  279.     
  280.     def process_unavailable_presence(self, stanza):
  281.         fr = stanza.get_from()
  282.         if not fr.resource:
  283.             return None
  284.         
  285.         nick = fr.resource
  286.         user = self.users.get(nick)
  287.         if user:
  288.             old_user = MucRoomUser(user)
  289.             user.update_presence(stanza)
  290.             self.handler.presence_changed(user, stanza)
  291.             if user.new_nick:
  292.                 mc = stanza.get_muc_child()
  293.                 if isinstance(mc, MucUserX):
  294.                     renames = _[1]
  295.                     if renames:
  296.                         self.users[user.new_nick] = user
  297.                         del self.users[nick]
  298.                         return None
  299.                     
  300.                 
  301.             
  302.         else:
  303.             old_user = None
  304.             user = MucRoomUser(stanza)
  305.             self.users[user.nick] = user
  306.             self.handler.presence_changed(user, stanza)
  307.         if fr == self.room_jid and self.joined:
  308.             self.joined = False
  309.             self.handler.user_left(user, stanza)
  310.             self.manager.forget(self)
  311.             self.me = user
  312.         elif old_user:
  313.             self.handler.user_left(user, stanza)
  314.         
  315.  
  316.     
  317.     def process_groupchat_message(self, stanza):
  318.         fr = stanza.get_from()
  319.         user = self.get_user(fr, True)
  320.         s = stanza.get_subject()
  321.         if s:
  322.             self.subject = s
  323.             self.handler.subject_changed(user, stanza)
  324.         else:
  325.             self.handler.message_received(user, stanza)
  326.  
  327.     
  328.     def process_error_message(self, stanza):
  329.         self.handler.error(stanza)
  330.  
  331.     
  332.     def process_error_presence(self, stanza):
  333.         self.handler.error(stanza)
  334.  
  335.     
  336.     def process_configuration_form_success(self, stanza):
  337.         if stanza.get_query_ns() != MUC_OWNER_NS:
  338.             raise ValueError, 'Bad result namespace'
  339.         
  340.         query = stanza.get_query()
  341.         form = None
  342.         for el in xml_element_ns_iter(query.children, DATAFORM_NS):
  343.             form = Form(el)
  344.             break
  345.         
  346.         if not form:
  347.             raise ValueError, 'No form received'
  348.         
  349.         self.configuration_form = form
  350.         self.handler.configuration_form_received(form)
  351.  
  352.     
  353.     def process_configuration_form_error(self, stanza):
  354.         self.handler.error(stanza)
  355.  
  356.     
  357.     def request_configuration_form(self):
  358.         iq = Iq(to_jid = self.room_jid.bare(), stanza_type = 'get')
  359.         iq.new_query(MUC_OWNER_NS, 'query')
  360.         self.manager.stream.set_response_handlers(iq, self.process_configuration_form_success, self.process_configuration_form_error)
  361.         self.manager.stream.send(iq)
  362.         return iq.get_id()
  363.  
  364.     
  365.     def process_configuration_success(self, stanza):
  366.         _unused = stanza
  367.         self.configured = True
  368.         self.handler.room_configured()
  369.  
  370.     
  371.     def process_configuration_error(self, stanza):
  372.         self.handler.room_configuration_error(stanza)
  373.  
  374.     
  375.     def configure_room(self, form):
  376.         if form.type == 'cancel':
  377.             return None
  378.         elif form.type != 'submit':
  379.             raise ValueError, "A 'submit' form required to configure a room"
  380.         
  381.         iq = Iq(to_jid = self.room_jid.bare(), stanza_type = 'set')
  382.         query = iq.new_query(MUC_OWNER_NS, 'query')
  383.         form.as_xml(query)
  384.         self.manager.stream.set_response_handlers(iq, self.process_configuration_success, self.process_configuration_error)
  385.         self.manager.stream.send(iq)
  386.         return iq.get_id()
  387.  
  388.     
  389.     def request_instant_room(self):
  390.         if self.configured:
  391.             raise RuntimeError, 'Instant room may be requested for unconfigured room only'
  392.         
  393.         form = Form('submit')
  394.         return self.configure_room(form)
  395.  
  396.  
  397.  
  398. class MucRoomManager:
  399.     
  400.     def __init__(self, stream):
  401.         self.rooms = { }
  402.         (self.stream, self.jid) = (None, None)
  403.         self.set_stream(stream)
  404.         self._MucRoomManager__logger = logging.getLogger('pyxmpp.jabber.MucRoomManager')
  405.  
  406.     
  407.     def set_stream(self, stream):
  408.         self.jid = stream.me
  409.         self.stream = stream
  410.         for r in self.rooms.values():
  411.             r.set_stream(stream)
  412.         
  413.  
  414.     
  415.     def set_handlers(self, priority = 10):
  416.         self.stream.set_message_handler('groupchat', self._MucRoomManager__groupchat_message, None, priority)
  417.         self.stream.set_message_handler('error', self._MucRoomManager__error_message, None, priority)
  418.         self.stream.set_presence_handler('available', self._MucRoomManager__presence_available, None, priority)
  419.         self.stream.set_presence_handler('unavailable', self._MucRoomManager__presence_unavailable, None, priority)
  420.         self.stream.set_presence_handler('error', self._MucRoomManager__presence_error, None, priority)
  421.  
  422.     
  423.     def join(self, room, nick, handler, password = None, history_maxchars = None, history_maxstanzas = None, history_seconds = None, history_since = None):
  424.         if not (room.node) or room.resource:
  425.             raise ValueError, 'Invalid room JID'
  426.         
  427.         room_jid = JID(room.node, room.domain, nick)
  428.         cur_rs = self.rooms.get(room_jid.bare().as_unicode())
  429.         if cur_rs and cur_rs.joined:
  430.             raise RuntimeError, 'Room already joined'
  431.         
  432.         rs = MucRoomState(self, self.stream.me, room_jid, handler)
  433.         self.rooms[room_jid.bare().as_unicode()] = rs
  434.         rs.join(password, history_maxchars, history_maxstanzas, history_seconds, history_since)
  435.         return rs
  436.  
  437.     
  438.     def get_room_state(self, room):
  439.         return self.rooms.get(room.bare().as_unicode())
  440.  
  441.     
  442.     def forget(self, rs):
  443.         
  444.         try:
  445.             del self.rooms[rs.room_jid.bare().as_unicode()]
  446.         except KeyError:
  447.             pass
  448.  
  449.  
  450.     
  451.     def __groupchat_message(self, stanza):
  452.         fr = stanza.get_from()
  453.         key = fr.bare().as_unicode()
  454.         rs = self.rooms.get(key)
  455.         if not rs:
  456.             self._MucRoomManager__logger.debug('groupchat message from unknown source')
  457.             return False
  458.         
  459.         rs.process_groupchat_message(stanza)
  460.         return True
  461.  
  462.     
  463.     def __error_message(self, stanza):
  464.         fr = stanza.get_from()
  465.         key = fr.bare().as_unicode()
  466.         rs = self.rooms.get(key)
  467.         if not rs:
  468.             return False
  469.         
  470.         rs.process_error_message(stanza)
  471.         return True
  472.  
  473.     
  474.     def __presence_error(self, stanza):
  475.         fr = stanza.get_from()
  476.         key = fr.bare().as_unicode()
  477.         rs = self.rooms.get(key)
  478.         if not rs:
  479.             return False
  480.         
  481.         rs.process_error_presence(stanza)
  482.         return True
  483.  
  484.     
  485.     def __presence_available(self, stanza):
  486.         fr = stanza.get_from()
  487.         key = fr.bare().as_unicode()
  488.         rs = self.rooms.get(key)
  489.         if not rs:
  490.             return False
  491.         
  492.         rs.process_available_presence(MucPresence(stanza))
  493.         return True
  494.  
  495.     
  496.     def __presence_unavailable(self, stanza):
  497.         fr = stanza.get_from()
  498.         key = fr.bare().as_unicode()
  499.         rs = self.rooms.get(key)
  500.         if not rs:
  501.             return False
  502.         
  503.         rs.process_unavailable_presence(MucPresence(stanza))
  504.         return True
  505.  
  506.  
  507.